/**
 * tarside-ftp -- A simple FTP client, using the ANSI C and GTK+.
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "local_dir.h"
#include "local_file.h"

static void on_changed(GtkWidget *widget, gpointer label);
static gint double_click(GtkWidget *widget, GdkEvent *event);
static OnListSelected on_select = &on_changed;
static OnDoubleClick on_double_click = &double_click;
static GtkWidget *g_dir_list;

static void init_local_files(GtkWidget * window) {
	g_dir_list = gtk_tree_view_new();
	init_list(g_dir_list);
	init_list_view(window, g_dir_list, on_select, on_double_click);
	char cwd[255];
	getcwd(cwd, 255);
	set_local_current_dir(cwd);
}

static void on_changed(GtkWidget *widget, gpointer label) {
	GtkTreeIter iter;
	GtkTreeModel *model;
	if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter)) {
		char *value;
		gtk_tree_model_get(model, &iter, 0, &value, -1);
		char real_path[255] = { '\0' };
		sprintf(real_path, "%s/%s", get_local_current_dir(), value);
		char* real_path1 = realpath(real_path, NULL);
		g_print(" on changed %s\n", real_path1);
		g_free(value);
	}
}

static void padding_dirname(char* cwd) {
	clean_list(g_dir_list);
	DIR *pdir = NULL;
	if ((pdir = opendir(cwd)) == NULL) {
		printf("open dir error! %s \n", cwd);
	} else {
		struct dirent* entry;
		while (entry = readdir(pdir)) {
				if (DT_DIR == entry->d_type) {
					add_to_list(g_dir_list, entry->d_name);
				}
		}
		closedir(pdir);
	}
}
static gint double_click(GtkWidget *widget, GdkEvent *event) {
	GdkEventButton *bevent = (GdkEventButton *) event;
	if (event->type == GDK_2BUTTON_PRESS) {
		GtkTreeIter iter;
		GtkTreeModel *model;
		if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter)) {
			char *value;
			gtk_tree_model_get(model, &iter, 0, &value, -1);
			char real_path[255] = { '\0' };
			sprintf(real_path, "%s/%s", get_local_current_dir(), value);
			char* cwd = realpath(real_path, NULL);
			g_print(" on double_click %s\n", cwd);
			padding_dirname(cwd);
			padding_filename(cwd);
			g_free(value);
		}
		return TRUE;
	}
	return FALSE;
}


static void local_dir_tree(GtkWidget* paned) {
	GtkWidget *vbox = gtk_vbox_new(FALSE, 1);
	gtk_paned_add1(GTK_PANED(paned), vbox);
	GtkWidget *valign = gtk_alignment_new(1, 0, 0, 0);
	gtk_box_pack_start(GTK_BOX(vbox), valign, FALSE, FALSE, 0);

	init_local_files(vbox);
	padding_dirname(get_local_current_dir());
}


void local(GtkWidget *paned) {
	GtkWidget *paned1 = gtk_vpaned_new();/*创建纵向分栏窗口构件*/
	gtk_container_add(GTK_CONTAINER(paned), paned1);/*将分栏窗口构件加入窗体*/
	gtk_widget_show(paned1);
	local_dir_tree(paned1);
	local_dir_file(paned1);
}

void refresh_local_dir(){
	padding_dirname(get_local_current_dir());
	padding_filename(get_local_current_dir());
}
